package cn.sdnu.database.utils;

/**
 * @author Master_Joe qiaoguangtong@aliyun.com
 */
public class SqlConvert {

	public static final String NEW_LINE = System.getProperty("line.separator");

	/**
	 * 将用户输入的SQL 语句转化为统一格式的SQL, 适合我们的编程
	 * 
	 * @param sql 用户输入的乱格式的SQL
	 * @return 正常的SQL
	 */
	public static String convert(String sql) {
		sql = sql.replaceAll("NEW_LINE", "");
		sql = sql.replaceAll("\n", "");
		sql = sql.replaceAll("\t", " ");
		sql = sql.replaceAll("\r", " ");
		sql = sql.replaceAll("\\( ", "(");
		sql = sql.replaceAll(",", ", ");
		sql = sql.replaceAll("=", " = ");
		sql = sql.replaceAll("and", " and ");
		sql = sql.replaceAll(" +", " ");
		sql = sql.replaceAll("^ *", "");
		sql = sql.replaceAll(" ,", ",");
		sql = sql.replaceAll(" *;", ";");
		return sql;
	}
}
